﻿using SD.Toolkits.Mapper;
using SlamDunk.MES.Domain.Entities.StockContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Maps
{
    /// <summary>
    /// 库存相关映射
    /// </summary>
    public static class StockMap
    {
        #region # 物料批次映射 —— static MaterialLotInfo ToDTO(this MaterialLot...
        /// <summary>
        /// 物料批次映射
        /// </summary>
        public static MaterialLotInfo ToDTO(this MaterialLot materialLot, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            MaterialLotInfo materialLotInfo = materialLot.Map<MaterialLot, MaterialLotInfo>();
            materialLotInfo.FacilityInfo = materialLot.FacilityId.HasValue
                ? facilityInfos?[materialLot.FacilityId.Value]
                : null;

            return materialLotInfo;
        }
        #endregion

        #region # 物料条码映射 —— static MaterialBarcodeInfo ToDTO(this MaterialBarcode...
        /// <summary>
        /// 物料条码映射
        /// </summary>
        public static MaterialBarcodeInfo ToDTO(this MaterialBarcode materialBarcode, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            MaterialBarcodeInfo materialBarcodeInfo = materialBarcode.Map<MaterialBarcode, MaterialBarcodeInfo>();
            materialBarcodeInfo.MaterialInfo = materialInfos?[materialBarcode.MaterialId];
            materialBarcodeInfo.MaterialLotInfo = materialBarcode.MaterialLotId.HasValue
                ? materialLotInfos?[materialBarcode.MaterialLotId.Value]
                : null;
            materialBarcodeInfo.FacilityInfo = materialBarcode.FacilityId.HasValue
                ? facilityInfos?[materialBarcode.FacilityId.Value]
                : null;

            return materialBarcodeInfo;
        }
        #endregion

        #region # 库存结转记录映射 —— static StockSettlementInfo ToDTO(this StockSettlement...
        /// <summary>
        /// 库存结转记录映射
        /// </summary>
        public static StockSettlementInfo ToDTO(this StockSettlement stockSettlement, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            StockSettlementInfo stockSettlementInfo = stockSettlement.Map<StockSettlement, StockSettlementInfo>();
            stockSettlementInfo.MaterialInfo = materialInfos?[stockSettlement.MaterialId];
            stockSettlementInfo.MaterialLotInfo = stockSettlement.MaterialLotId.HasValue
                ? materialLotInfos?[stockSettlement.MaterialLotId.Value]
                : null;
            stockSettlementInfo.FacilityInfo = stockSettlement.FacilityId.HasValue
                ? facilityInfos?[stockSettlement.FacilityId.Value]
                : null;

            return stockSettlementInfo;
        }
        #endregion

        #region # 出/入库记录映射 —— static BoundRecordInfo ToDTO(this BoundRecord boundRecord...
        /// <summary>
        /// 出/入库记录映射
        /// </summary>
        public static BoundRecordInfo ToDTO(this BoundRecord boundRecord, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            BoundRecordInfo boundRecordInfo = boundRecord.Map<BoundRecord, BoundRecordInfo>();
            boundRecordInfo.MaterialInfo = materialInfos?[boundRecord.MaterialId];
            boundRecordInfo.MaterialLotInfo = boundRecord.MaterialLotId.HasValue
                ? materialLotInfos?[boundRecord.MaterialLotId.Value]
                : null;
            boundRecordInfo.FacilityInfo = boundRecord.FacilityId.HasValue
                ? facilityInfos?[boundRecord.FacilityId.Value]
                : null;

            return boundRecordInfo;
        }
        #endregion

        #region # 库存单据类映射 —— static StockOrderClassInfo ToDTO(this StockOrderClass...
        /// <summary>
        /// 库存单据类映射
        /// </summary>
        public static StockOrderClassInfo ToDTO(this StockOrderClass stockOrderClass, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            StockOrderClassInfo stockOrderClassInfo = stockOrderClass.Map<StockOrderClass, StockOrderClassInfo>();
            stockOrderClassInfo.FacilityInfo = stockOrderClass.FacilityId.HasValue
                ? facilityInfos?[stockOrderClass.FacilityId.Value]
                : null;

            return stockOrderClassInfo;
        }
        #endregion

        #region # 库存单据映射 —— static StockOrderInfo ToDTO(this StockOrder stockOrder...
        /// <summary>
        /// 库存单据映射
        /// </summary>
        public static StockOrderInfo ToDTO(this StockOrder stockOrder, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            StockOrderInfo stockOrderInfo = stockOrder.Map<StockOrder, StockOrderInfo>();
            stockOrderInfo.FacilityInfo = stockOrder.FacilityId.HasValue
                ? facilityInfos?[stockOrder.FacilityId.Value]
                : null;

            return stockOrderInfo;
        }
        #endregion

        #region # 入库单映射 —— static StockInOrderInfo ToDTO(this StockInOrder...
        /// <summary>
        /// 入库单映射
        /// </summary>
        public static StockInOrderInfo ToDTO(this StockInOrder stockInOrder, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            StockInOrderInfo orderInfo = stockInOrder.Map<StockInOrder, StockInOrderInfo>();
            orderInfo.FacilityInfo = stockInOrder.FacilityId.HasValue
                ? facilityInfos?[stockInOrder.FacilityId.Value]
                : null;

            return orderInfo;
        }
        #endregion

        #region # 出库单映射 —— static StockOutOrderInfo ToDTO(this StockOutOrder...
        /// <summary>
        /// 出库单映射
        /// </summary>
        public static StockOutOrderInfo ToDTO(this StockOutOrder stockOutOrder, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            StockOutOrderInfo orderInfo = stockOutOrder.Map<StockOutOrder, StockOutOrderInfo>();
            orderInfo.FacilityInfo = stockOutOrder.FacilityId.HasValue
                ? facilityInfos?[stockOutOrder.FacilityId.Value]
                : null;

            return orderInfo;
        }
        #endregion

        #region # 库存单据明细映射 —— static StockOrderDetailInfo ToDTO(this StockOrderDetail...
        /// <summary>
        /// 库存单据明细映射
        /// </summary>
        public static StockOrderDetailInfo ToDTO(this StockOrderDetail detail, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos)
        {
            StockOrderDetailInfo detailInfo = detail.Map<StockOrderDetail, StockOrderDetailInfo>();
            detailInfo.MaterialInfo = materialInfos?[detail.MaterialId];
            detailInfo.MaterialLotInfo = detail.MaterialLotId.HasValue
                ? materialLotInfos?[detail.MaterialLotId.Value]
                : null;

            return detailInfo;
        }
        #endregion
    }
}
